package com.minishift.android.utils;

import java.io.IOException;
import java.util.List;


import android.content.Context;
import android.location.Address;
import android.location.Criteria;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;

/**
 * 
 * this class is uesd to get current location.
 * @author Sirier
 *
 */
public class XLocation {

	private static XLocation instance;
	private static Context context;
	private static final String SERVICE_NAME = Context.LOCATION_SERVICE;

	private LocationManager locationManager;

	private XLocation(){}

	/**
	 * this method will get only object of XLocation.
	 * @param Context: current application.
	 * @return InitAction: only object of XLcation.
	 */
	public static XLocation getXLocation(Context c) {
		if (instance == null) {
			instance = new XLocation();
		}
		if (context == null) {
			context = c;
		}
		return instance;
	}

	/**
	 * this method will get current location.
	 * @return String: current location.
	 */
	public String getLocation() {
		locationManager = (LocationManager) context.getSystemService(SERVICE_NAME);
		Criteria criteria = new Criteria();
		if(XNetworkStatus.STATUS_WIFI == XNetworkStatus.getNetworkStatus(context)){
			criteria.setAccuracy(Criteria.ACCURACY_COARSE);			
		} else if (XNetworkStatus.STATUS_MOBILE == XNetworkStatus.getNetworkStatus(context)){
			criteria.setAccuracy(Criteria.ACCURACY_FINE);
		} else {
			
		}
		criteria.setAltitudeRequired(false);
		criteria.setBearingRequired(false);
		criteria.setCostAllowed(true);
		criteria.setPowerRequirement(Criteria.POWER_LOW);
		Location location = locationManager.getLastKnownLocation(locationManager.getBestProvider(criteria, true));
		locationManager.requestLocationUpdates(locationManager.getBestProvider(criteria, true), 2000, 10,
				locationListener);
		return updateWithNewLocation(location);
	}

	/**
	 * location listener
	 */
	private final LocationListener locationListener = new LocationListener() {
		public void onLocationChanged(Location location) {
			updateWithNewLocation(location);
		}

		public void onProviderDisabled(String provider) {
			updateWithNewLocation(null);
		}

		public void onProviderEnabled(String provider) {
		}

		public void onStatusChanged(String provider, int status, Bundle extras) {
		}
	};

	/**
	 * this method will update device location.
	 * @param Location: current location.
	 * @return String: curretn city.
	 */
	private String updateWithNewLocation(Location location) {
		String locationStr = null;
		double lat = 0;
		double lng = 0;
		if (location != null) {
			lat = location.getLatitude();
			lng = location.getLongitude();
		} else {
			locationStr = "no city";
		}
		if(0 == lat && 0 == lng){
			return locationStr;
		}
		List<Address> addList = null;
		Geocoder geocoder = new Geocoder(context);
		try {
			addList = geocoder.getFromLocation(lat, lng, 1);
		} catch (IOException e) {
			XLog.e(SERVICE_NAME, e.toString());
		}
		if (addList != null && addList.size() > 0) {
			locationStr = addList.get(0).getLocality();
		}
		return locationStr;
	}
}
